﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using NewLife;
using NewLife.Data;
using XCode;
using XCode.Cache;
using XCode.Configuration;
using XCode.DataAccessLayer;

namespace EHO.AutoUpdate.Server.Database;

/// <summary>程序更新版本信息</summary>
[Serializable]
[DataObject]
[Description("程序更新版本信息")]
[BindIndex("PK_app_update_version_AppId_Version", true, "AppId,Version")]
[BindIndex("IX_app_update_version_AppId_PubTime", false, "AppId,PubTime")]
[BindTable("app_update_version", Description = "程序更新版本信息", ConnName = "AutoUpdate", DbType = DatabaseType.SqlServer)]
public partial class AppUpdateVersion
{
    #region 属性
    private Int64 _Id;
    /// <summary>主键Id</summary>
    [DisplayName("主键Id")]
    [Description("主键Id")]
    [DataObjectField(true, false, false, 0)]
    [BindColumn("Id", "主键Id", "", DataScale = "time")]
    public Int64 Id { get => _Id; set { if (OnPropertyChanging("Id", value)) { _Id = value; OnPropertyChanged("Id"); } } }

    private Int64 _AppId;
    /// <summary>编码</summary>
    [DisplayName("编码")]
    [Description("编码")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("AppId", "编码", "")]
    public Int64 AppId { get => _AppId; set { if (OnPropertyChanging("AppId", value)) { _AppId = value; OnPropertyChanged("AppId"); } } }

    private Int32 _ClientType;
    /// <summary>1:主程序 2、更新组件</summary>
    [DisplayName("1")]
    [Description("1:主程序 2、更新组件")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("ClientType", "1:主程序 2、更新组件", "")]
    public Int32 ClientType { get => _ClientType; set { if (OnPropertyChanging("ClientType", value)) { _ClientType = value; OnPropertyChanged("ClientType"); } } }

    private Int64 _PubTime;
    /// <summary>发送时间</summary>
    [DisplayName("发送时间")]
    [Description("发送时间")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("PubTime", "发送时间", "")]
    public Int64 PubTime { get => _PubTime; set { if (OnPropertyChanging("PubTime", value)) { _PubTime = value; OnPropertyChanged("PubTime"); } } }

    private String _Md5;
    /// <summary>md5</summary>
    [DisplayName("md5")]
    [Description("md5")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("Md5", "md5", "")]
    public String Md5 { get => _Md5; set { if (OnPropertyChanging("Md5", value)) { _Md5 = value; OnPropertyChanged("Md5"); } } }

    private String _Path;
    /// <summary>路径</summary>
    [DisplayName("路径")]
    [Description("路径")]
    [DataObjectField(false, false, true, 200)]
    [BindColumn("Path", "路径", "")]
    public String Path { get => _Path; set { if (OnPropertyChanging("Path", value)) { _Path = value; OnPropertyChanged("Path"); } } }

    private String _Version;
    /// <summary>版本</summary>
    [DisplayName("版本")]
    [Description("版本")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("Version", "版本", "")]
    public String Version { get => _Version; set { if (OnPropertyChanging("Version", value)) { _Version = value; OnPropertyChanged("Version"); } } }

    private String _Description;
    /// <summary>备注</summary>
    [DisplayName("备注")]
    [Description("备注")]
    [DataObjectField(false, false, true, 200)]
    [BindColumn("Description", "备注", "")]
    public String Description { get => _Description; set { if (OnPropertyChanging("Description", value)) { _Description = value; OnPropertyChanged("Description"); } } }

    private DateTime _CreateTime;
    /// <summary>创建日期</summary>
    [DisplayName("创建日期")]
    [Description("创建日期")]
    [DataObjectField(false, false, true, 0)]
    [BindColumn("CreateTime", "创建日期", "", Precision = 0, Scale = 3)]
    public DateTime CreateTime { get => _CreateTime; set { if (OnPropertyChanging("CreateTime", value)) { _CreateTime = value; OnPropertyChanged("CreateTime"); } } }

    private Int64 _CreateUserID;
    /// <summary>创建用户主键</summary>
    [DisplayName("创建用户主键")]
    [Description("创建用户主键")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("CreateUserID", "创建用户主键", "")]
    public Int64 CreateUserID { get => _CreateUserID; set { if (OnPropertyChanging("CreateUserID", value)) { _CreateUserID = value; OnPropertyChanged("CreateUserID"); } } }

    private String _CreateUser;
    /// <summary>创建用户</summary>
    [DisplayName("创建用户")]
    [Description("创建用户")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("CreateUser", "创建用户", "")]
    public String CreateUser { get => _CreateUser; set { if (OnPropertyChanging("CreateUser", value)) { _CreateUser = value; OnPropertyChanged("CreateUser"); } } }

    private String _CreateIP;
    /// <summary>创建地址</summary>
    [DisplayName("创建地址")]
    [Description("创建地址")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("CreateIP", "创建地址", "")]
    public String CreateIP { get => _CreateIP; set { if (OnPropertyChanging("CreateIP", value)) { _CreateIP = value; OnPropertyChanged("CreateIP"); } } }
    #endregion

    #region 获取/设置 字段值
    /// <summary>获取/设置 字段值</summary>
    /// <param name="name">字段名</param>
    /// <returns></returns>
    public override Object this[String name]
    {
        get => name switch
        {
            "Id" => _Id,
            "AppId" => _AppId,
            "ClientType" => _ClientType,
            "PubTime" => _PubTime,
            "Md5" => _Md5,
            "Path" => _Path,
            "Version" => _Version,
            "Description" => _Description,
            "CreateTime" => _CreateTime,
            "CreateUserID" => _CreateUserID,
            "CreateUser" => _CreateUser,
            "CreateIP" => _CreateIP,
            _ => base[name]
        };
        set
        {
            switch (name)
            {
                case "Id": _Id = value.ToLong(); break;
                case "AppId": _AppId = value.ToLong(); break;
                case "ClientType": _ClientType = value.ToInt(); break;
                case "PubTime": _PubTime = value.ToLong(); break;
                case "Md5": _Md5 = Convert.ToString(value); break;
                case "Path": _Path = Convert.ToString(value); break;
                case "Version": _Version = Convert.ToString(value); break;
                case "Description": _Description = Convert.ToString(value); break;
                case "CreateTime": _CreateTime = value.ToDateTime(); break;
                case "CreateUserID": _CreateUserID = value.ToLong(); break;
                case "CreateUser": _CreateUser = Convert.ToString(value); break;
                case "CreateIP": _CreateIP = Convert.ToString(value); break;
                default: base[name] = value; break;
            }
        }
    }
    #endregion

    #region 关联映射
    #endregion

    #region 扩展查询
    /// <summary>根据主键Id查找</summary>
    /// <param name="id">主键Id</param>
    /// <returns>实体对象</returns>
    public static AppUpdateVersion FindById(Int64 id)
    {
        if (id < 0) return null;

        return Find(_.Id == id);
    }

    /// <summary>根据编码、版本查找</summary>
    /// <param name="appId">编码</param>
    /// <param name="version">版本</param>
    /// <returns>实体对象</returns>
    public static AppUpdateVersion FindByAppIdAndVersion(Int64 appId, String version)
    {
        if (appId < 0) return null;
        if (version.IsNullOrEmpty()) return null;

        return Find(_.AppId == appId & _.Version == version);
    }

    /// <summary>根据编码查找</summary>
    /// <param name="appId">编码</param>
    /// <returns>实体列表</returns>
    public static IList<AppUpdateVersion> FindAllByAppId(Int64 appId)
    {
        if (appId < 0) return [];

        return FindAll(_.AppId == appId);
    }

    /// <summary>根据编码、发送时间查找</summary>
    /// <param name="appId">编码</param>
    /// <param name="pubTime">发送时间</param>
    /// <returns>实体列表</returns>
    public static IList<AppUpdateVersion> FindAllByAppIdAndPubTime(Int64 appId, Int64 pubTime)
    {
        if (appId < 0) return [];
        if (pubTime < 0) return [];

        return FindAll(_.AppId == appId & _.PubTime == pubTime);
    }
    #endregion

    #region 数据清理
    /// <summary>清理指定时间段内的数据</summary>
    /// <param name="start">开始时间。未指定时清理小于指定时间的所有数据</param>
    /// <param name="end">结束时间</param>
    /// <returns>清理行数</returns>
    public static Int32 DeleteWith(DateTime start, DateTime end)
    {
        return Delete(_.Id.Between(start, end, Meta.Factory.Snow));
    }
    #endregion

    #region 字段名
    /// <summary>取得程序更新版本信息字段信息的快捷方式</summary>
    public partial class _
    {
        /// <summary>主键Id</summary>
        public static readonly Field Id = FindByName("Id");

        /// <summary>编码</summary>
        public static readonly Field AppId = FindByName("AppId");

        /// <summary>1:主程序 2、更新组件</summary>
        public static readonly Field ClientType = FindByName("ClientType");

        /// <summary>发送时间</summary>
        public static readonly Field PubTime = FindByName("PubTime");

        /// <summary>md5</summary>
        public static readonly Field Md5 = FindByName("Md5");

        /// <summary>路径</summary>
        public static readonly Field Path = FindByName("Path");

        /// <summary>版本</summary>
        public static readonly Field Version = FindByName("Version");

        /// <summary>备注</summary>
        public static readonly Field Description = FindByName("Description");

        /// <summary>创建日期</summary>
        public static readonly Field CreateTime = FindByName("CreateTime");

        /// <summary>创建用户主键</summary>
        public static readonly Field CreateUserID = FindByName("CreateUserID");

        /// <summary>创建用户</summary>
        public static readonly Field CreateUser = FindByName("CreateUser");

        /// <summary>创建地址</summary>
        public static readonly Field CreateIP = FindByName("CreateIP");

        static Field FindByName(String name) => Meta.Table.FindByName(name);
    }

    /// <summary>取得程序更新版本信息字段名称的快捷方式</summary>
    public partial class __
    {
        /// <summary>主键Id</summary>
        public const String Id = "Id";

        /// <summary>编码</summary>
        public const String AppId = "AppId";

        /// <summary>1:主程序 2、更新组件</summary>
        public const String ClientType = "ClientType";

        /// <summary>发送时间</summary>
        public const String PubTime = "PubTime";

        /// <summary>md5</summary>
        public const String Md5 = "Md5";

        /// <summary>路径</summary>
        public const String Path = "Path";

        /// <summary>版本</summary>
        public const String Version = "Version";

        /// <summary>备注</summary>
        public const String Description = "Description";

        /// <summary>创建日期</summary>
        public const String CreateTime = "CreateTime";

        /// <summary>创建用户主键</summary>
        public const String CreateUserID = "CreateUserID";

        /// <summary>创建用户</summary>
        public const String CreateUser = "CreateUser";

        /// <summary>创建地址</summary>
        public const String CreateIP = "CreateIP";
    }
    #endregion
}
